package remote.controls
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	
	import remote.geom.Size;
	
	/**
	 * 图片
	 * 源可以是字符串，显示对象，或者显示对象类
	 * 可选是否缩放子内容到组件大小，如果不缩放，则只会显示一部分内容。
	 * @author 银魂飞雪
	 * @createDate 2011-4-13
	 */
	public class Image extends Component
	{
		private var _loader:Loader;
		private var _content:DisplayObject;
		private var _source:Object;
		private var _scaleContent:Boolean = true;
		public function Image()
		{
			super();
		}
		
		override public function updateSize():void
		{
			var oldSize:Size = new Size(width, height);
			super.updateSize();
			if(!oldSize.equal(new Size(width, height)))
			{
				this.updateChildren();
				this.scrollRect = new Rectangle(0, 0, width, height);
			}
				
		}
		
		override protected function updateChildren():void
		{
			super.updateChildren();
			if(_content != null)
			{
				if(_scaleContent)
				{
					var scaleX:Number = width / (_content.width / _content.scaleX);
					var scaleY:Number = height / (_content.height / _content.scaleY);
					var scale:Number = Math.min(scaleX, scaleY);
					_content.scaleX = scale;
					_content.scaleY = scale;
				}
				else
				{
					_content.scaleX = 1;
					_content.scaleY = 1;
				}
			}
		}
		
		override public function getMeasureSize():Size
		{
			if(_content != null)
				return new Size(_content.width, _content.height);
			return new Size(10, 10);
				
		}
		
		private function changeContent(newValue:DisplayObject):void
		{
			if(_content != newValue)
			{
				if(_content != null)
				{
					removeChild(_content);
				}
				_content = newValue;
				if(_content != null)
				{
					addChild(_content);
					this.updateChildren();
				}
			}
		}
		
		private function load():void
		{
			if(_loader == null)
			{
				_loader = new Loader();
				_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_completeHandler);
			}
			_loader.load(new URLRequest(_source as String));
		}
		
		private function loader_completeHandler(e:Event):void
		{
			changeContent((e.currentTarget as LoaderInfo).content);
		}

		/**
		 * 作为内容加载的 URL、对象、类或类的字符串名称。 
		 * 
		 */		
		public function get source():Object
		{
			return _source;
		}

		public function set source(value:Object):void
		{
			if(_source != value)
			{
				_source = value;
				if(_source == null)
				{
					changeContent(null);
				}
				else if(_source is String)
				{
					load();
				}
				else if(_source is Class)
				{
					changeContent(new _source());
				}
				else if(_source is DisplayObject)
				{
					changeContent(_source as DisplayObject);
				}
			}
		}

		public function get scaleContent():Boolean
		{
			return _scaleContent;
		}

		public function set scaleContent(value:Boolean):void
		{
			if(_scaleContent != value)
			{
				_scaleContent = value;
				this.updateChildren();
			}
		}


	}
}